﻿using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour
{

    private bool move;
    private bool moveStatic;
    private Vector3 moveDirection;
    public Transform moveTarget;
    public bool alive;

    void Awake ()
    {
        move = false;
        moveStatic = false;
        alive = true;
    }

    void Update ()
    {
        if (alive)
        {
            GetInput ();
        }

        //通过moveTarget移动
        if (move)
        {
            if (!moveStatic)
            {
                moveStatic = true;
                moveTarget.position = this.transform.position + moveDirection;
                moveTarget.position = new Vector3 (Mathf.RoundToInt (moveTarget.position.x),
                                                   Mathf.RoundToInt (moveTarget.position.y),
                                                   Mathf.RoundToInt (moveTarget.position.z));
                moveTarget.gameObject.GetComponent<MoveTarget> ().StartMove ();
                moveTarget.gameObject.GetComponent<Rigidbody> ().detectCollisions = true;
                if (moveDirection == Vector3.left)
                {
                    this.transform.rotation = GameController.gameController.left;
                }
                if (moveDirection == Vector3.right)
                {
                    this.transform.rotation = GameController.gameController.right;
                }
                if (moveDirection == Vector3.back)
                {
                    this.transform.rotation = GameController.gameController.back;
                }
                if (moveDirection == Vector3.forward)
                {
                    this.transform.rotation = GameController.gameController.forward;
                }
            }
        }

    }

    //获取输入
    public void GetInput ()
    {
        
        if (Input.GetKeyDown (KeyCode.A))
        {
            moveDirection = Vector3.left;
            move = true;
        }
        
        if (Input.GetKeyDown (KeyCode.D))
        {
            moveDirection = Vector3.right;
            move = true;
        }
        
        if (Input.GetKeyDown (KeyCode.W))
        {
            moveDirection = Vector3.forward;
            move = true;
        }
        
        if (Input.GetKeyDown (KeyCode.S))
        {
            moveDirection = Vector3.back;
            move = true;
        }
    }

    //停止移动
    public void StopMove ()
    {
        moveStatic = false;
    }

}
